package game.service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.sghd.common.utils.json.JsonUtils;
import data.dao.MysqlTemplate;
import data.entity.GameLine;
import game.model.ChessType;
import game.model.ConfigRoom;
import game.rule.Rule;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author fank
 * @date 2018-08-18 14:20
 * @desc
 **/
@Component
public class ConfigService {
    /** 所有房间配置缓存*/
    protected ConcurrentMap<Integer, ConfigRoom> configRooms = new ConcurrentHashMap<>();
    @Autowired
    protected MysqlTemplate querier;

    @PostConstruct
    void init() {
        load();
        DetachedCriteria criteria = DetachedCriteria.forClass(GameLine.class);
        criteria.addOrder(Order.asc("id"));
        List<GameLine> gameLines = querier.find(criteria);
        //初始化连线
        Rule.init(gameLines);
        //初始化元素属性
        ChessType.init(configRooms.values());
    }

    private void load() {
        List<data.entity.ConfigRoom> configs = querier.findAll(data.entity.ConfigRoom.class);
        for (data.entity.ConfigRoom config : configs) {
            ConfigRoom configRoom = new ConfigRoom();
            configRoom.setId(config.getId());
            configRoom.setGameId(config.getGameId());
            configRoom.setRoomName(config.getRoomName());
            configRoom.setFreeCount(config.getFreeCount());
            configRoom.setRoomId(config.getRoomId());
            configRoom.setDeskCount(config.getDeskCount());
            String rates = config.getRates();
            Integer[] rateArray = JsonUtils.string2Array(rates, Integer.class);
            configRoom.setRates(rateArray);
            String betArea = config.getBetArea();
            Integer[] betAreaArray = JsonUtils.string2Array(betArea, Integer.class);
            configRoom.setBetArea(betAreaArray);
            String limitMoney = config.getLimitMoney();
            Long[] limitMoneyArray = JsonUtils.string2Array(limitMoney, Long.class);
            configRoom.setLimitMoney(limitMoneyArray);
            String count2Rate = config.getCount2Rate();
            Map<Integer, Map<Integer, Integer>> count2RateArray = JsonUtils.string2GenericObject(count2Rate, new TypeReference<Map<Integer, Map<Integer, Integer>>>() {
            });
            configRoom.setCount2Rate(count2RateArray);
            configRoom.setRefund(config.getRefund());
            String rateFreeStr = config.getRatesFree();
            Integer[] ratesFree = JsonUtils.string2Array(rateFreeStr, Integer.class);
            configRoom.setRatesFree(ratesFree);
            String rewardRangst = config.getRewardRang();
            Integer[] rewardRang = JsonUtils.string2Array(rewardRangst, Integer.class);
            configRoom.setRewardRang(rewardRang);
            configRoom.setDrawWaters(config.getDrawWaters());
            String refundCtrlS = config.getRefundCtrl();
            Map<Integer, int[]> refundCtrl = JsonUtils.string2GenericObject(refundCtrlS, new TypeReference<Map<Integer, int[]>>() {
            });
            configRoom.setRefundCtrl(refundCtrl);
            String mgameStr = config.getMgame();
            Map<Integer, Integer> mgame = JsonUtils.string2Map(mgameStr, Integer.class, Integer.class);
            configRoom.setMgame(mgame);
            String dartStr = config.getDart();//飞镖
            Map<Integer, Integer> dart = JsonUtils.string2Map(dartStr, Integer.class, Integer.class);
            configRoom.setDart(dart);
            String flowerStr = config.getFlower();//花
            Map<Float, Integer> flower = JsonUtils.string2Map(flowerStr, Float.class, Integer.class);
            configRoom.setFlower(flower);
            String buy2Betstr = config.getBuy2Bet();
            Map<Integer, Integer> buy2Bet = JsonUtils.string2Map(buy2Betstr, Integer.class, Integer.class);
            configRoom.setBuy2Bet(buy2Bet);
            configRooms.put(config.getRoomId(), configRoom);
        }
    }

    public int getSize() {
        return configRooms.size();
    }

    public ConcurrentMap<Integer, ConfigRoom> getConfigRooms() {
        return configRooms;
    }
}
